package com.gopay.bis.manager.bank.ebank;

import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;

import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.common.Result;
import com.gopay.common.constants.bank.BankConstants;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.payease.ebank.PayEaseMessage;
import com.gopay.common.exception.GopayException;
import com.gopay.remote.bank.BankManagerRemoteService;
import com.gopay.remote.bank.businesses.BankService;

/**
 * 首信易支付网关直连对象封装
 * 
 * @PayEaseManager.java
 * @author zhengxue
 * @2015-9-1 下午2:49:13 www.gopay.com.cn Inc.All rights reserved.
 */
public class PayEaseManager implements BankManagerRemoteService {
    @Resource(name = "payEaseBankServiceClient")
    private BankService payEaseBankServiceClient;

    @Resource(name = "vpmode")
    private Map<String, String> vpmode;
    /**
     * 签名
     */
    public Object sign(BaseMessage message) throws GopayException {
        PayEaseMessage msg = buildSig(message);
        BankLogUtils.printB2CManagerSignSendLog(msg);
        String signMessage = (String) payEaseBankServiceClient.sign(msg);
        msg.setVmd5info(signMessage);
        BankLogUtils.printB2CManagerSignReturnLog(msg);
        return msg;
    }

    /**
     * 验签
     */
    public Object verify(BaseMessage message) throws GopayException {
    	message.setOutStlmCd(BankConstants.BANK_LOGO_PAYEASE);		//为了打日志
        BankLogUtils.printB2CManagerVerifySendLog(message);
        boolean flag = (Boolean) payEaseBankServiceClient.verify(message);
        BankLogUtils.printB2CManagerVerifyReturnLog(message, flag);
        return flag;
    }

    /**
     * 查询
     */
    public Object query(Object message) throws GopayException {
    	PayEaseMessage queryMsg = buildQuery((PayEaseMessage)message);
    	queryMsg.setOutStlmCd(BankConstants.BANK_LOGO_PAYEASE);		//为了打日志
    	BankLogUtils.printB2CManagerQuerySendLog(queryMsg, queryMsg.getPlainMessage());
    	Object query = payEaseBankServiceClient.query(queryMsg);
        return query;
    }

    @Override
    public Object dayend(BaseMessage arg0) throws GopayException, Exception {
        // TODO Auto-generated method stub
        return null;
    }

    /**
     * 构建签名信息
     * 
     * @param message
     * @return
     */
    private PayEaseMessage buildSig(BaseMessage message) {
        PayEaseMessage msg = new PayEaseMessage();
        BeanUtils.copyProperties(message, msg);
        
        String vmid = message.getMerID();
        // 判断是否使用小额商户
        if (msg.isSmallMer()) {
            vmid = message.getMerID2nd();
        }
        msg.setVmid(vmid);
        // 订单编号格式为：订单生成日期-商户编号-商户流水
        String vid = message.getOrderDate().substring(0, 8) + "-" + vmid + "-" + message.getOrderId();
        msg.setBankOrderId(vid);    //财务最后要求把银行订单号跟首信单号改成一致
        msg.setVid(vid);
        msg.setVrcvname(vmid);
        msg.setVrcvaddr(vmid);
        msg.setVrcvtel(vmid);
        msg.setVrcvpost(vmid);
        msg.setVamount(message.getAmount().toString());
        msg.setVymd(message.getOrderDate().substring(0, 8));
        msg.setVorderstatus("1"); // 配货状态 1:已配齐
        msg.setVordername(vmid);
        msg.setVmoneytype("0"); // 0:人民币
        msg.setVurl(message.getPayNotifyUrl());
        msg.setVpmode(vpmode.get(message.getBank()));
        // 签名明文
        String plain = msg.getVmoneytype() + msg.getVymd() + msg.getVamount() + msg.getVrcvname() + msg.getVid()
                + msg.getVmid() + msg.getVurl();
        msg.setPlainMessage(plain);
        msg.setOutStlmCd(message.getOutStlmCd());
        return msg;
    }

    /**
     * 构建查询信息
     * 
     * @param message
     * @return
     */
    private PayEaseMessage buildQuery(BaseMessage message) {
    	PayEaseMessage msg = new PayEaseMessage();
        BeanUtils.copyProperties(message, msg);
        msg.setVmid(message.getMerID());
        // 判断是否使用小额商户
        if (msg.isSmallMer()) {
            msg.setVmid(message.getMerID2nd());
        }
        // 订单编号格式为：订单生成日期-商户编号-商户流水
//        String vid = message.getOrderDate().substring(0, 8) + "-" + message.getMerID() + "-" + message.getOrderId();
        msg.setVid(message.getBankOrderId());
        // 签名明文
        String plain = msg.getVmid() + msg.getVid();
        msg.setPlainMessage(plain);
        return msg;
    }

	@Override
	public Object refund(BaseMessage base) throws GopayException {
		// TODO Auto-generated method stub
		return null;
	}

}
